package com.orifine.web;

import java.text.MessageFormat;
import java.util.Locale;


public class Property {
	private Domain key;
	private String message;
	private Object[] args;
	
	public Property(Domain key, Object... args){
		this.key = key;
		this.args = args;
	}
	
	public Property(String message){
		this.message = message;
	}
	
	public String toString(Internationalization internationalization){
		if(message == null){
			for(int i=0; i < args.length; i++){
				if(args[i] instanceof Property){
					args[i] = ((Property)args[i]).toString(internationalization);
				}
			}
			
			String dynamicKey = key.toString()+(args.length>1?"["+args.length+"]":"");
			String dynamicValue = internationalization.getMessage(dynamicKey);
			if(dynamicValue == null){
				dynamicValue = "'{"+dynamicKey+"}";
			}
			return MessageFormat.format(dynamicValue, args);
		} else {
			return message;
		}
	}
	
	public String toString(Internationalization internationalization, Locale locale){
		if(message == null){
			for(int i=0; i < args.length; i++){
				if(args[i] instanceof Property){
					args[i] = ((Property)args[i]).toString(internationalization, locale);
				}
			}
			
			String dynamicKey = key.toString()+(args.length>1?"["+args.length+"]":"");
			String dynamicValue = internationalization.getMessage(dynamicKey);
			if(dynamicValue == null){
				dynamicValue = "'{"+dynamicKey+"}";
			}
			return MessageFormat.format(dynamicValue, args);
		} else {
			return message;
		}
	}
}
